import django_filters
from django_filters.rest_framework import DjangoFilterBackend, FilterSet

from mysqldb.models import *


class ListDjangoFilterBackend(DjangoFilterBackend):
    """自己定义过滤器"""

    def get_schema_fields(self, view):
        if view.action not in ["list"]:
            return []
        return super().get_schema_fields(view)


class AllinpayPlaceinterListFilterSet(FilterSet):
    """员工积分列表帅选器"""

    pid = django_filters.NumberFilter(field_name="pid", required=False, label="场所ID")
    sid = django_filters.NumberFilter(field_name="sid", required=False, label="员工ID")
    intertype = django_filters.NumberFilter(
        field_name="intertype", required=False, label="积分类型ID"
    )
    # pid = django_filters.ModelMultipleChoiceFilter(
    #     field_name="placestaff",
    #     to_field_name="pid",
    #     queryset=AllinpayPlacestaffList.objects.all(),
    #     label="场所ID",
    # )

    class Meta:
        model = AllinpayPlaceinterList
        fields = ["pid", "sid", "intertype"]


class AllinpayPlaceinterLogFilterSet(FilterSet):
    """员工积分日志帅选器"""

    pid = django_filters.NumberFilter(field_name="pid", required=False, label="场所ID")
    sid = django_filters.NumberFilter(field_name="sid", required=False, label="员工ID")
    intertype = django_filters.NumberFilter(
        field_name="intertype", required=False, label="积分类型ID"
    )
    strdate = django_filters.DateTimeFilter(
        field_name="currenttime", required=False, method="filter_strdate", label="开始时间"
    )
    enddate = django_filters.DateTimeFilter(
        field_name="currenttime", required=False, method="filter_enddate", label="结束时间"
    )
    status = django_filters.NumberFilter(
        field_name="status", required=False, label="状态"
    )
    post = django_filters.CharFilter(field_name="post", required=False, label="兑换单")

    def filter_strdate(self, qs, name, value):
        lookup = "__".join([name, "gte"])
        return qs.filter(**{lookup: value})

    def filter_enddate(self, qs, name, value):
        lookup = "__".join([name, "lte"])
        return qs.filter(**{lookup: value})

    class Meta:
        model = AllinpayPlaceinterLog
        fields = ["pid", "sid", "intertype", "strdate", "enddate", "status", "post"]
